package com.cloudbroker.bsc.common.search;

import com.alibaba.fastjson.JSON;
import com.cloudbroker.bsc.common.search.common.SearchEngineAdapterClient;
import com.cloudbroker.bsc.common.search.elasticsearch.ElasticsearchEntity;

import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.junit.Test;

import java.net.InetAddress;
import java.util.*;


public class SearchEngineDemo {
    private final static String table_name  = "test_type";
    private final static String row_id    = "test_id1";
//
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	@Test
//	public void save(){
//
//		Map map = new HashMap();
//		map.put("name", "1111111");
//		map.put("date", 12222222223456l);
//		map.put("amount", "321");
//		System.out.println("add   " + SearchEngineAdapterClient.getSearchEngineAdapter().save(table_name, row_id, map));
//	}
//
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	@Test
//	public void save_list(){
//		List<ElasticsearchEntity> list = new ArrayList();
//		ElasticsearchEntity entity = new ElasticsearchEntity();
//		entity.setTable_name(table_name);
//		entity.setRow_id(row_id);
//		Map map = new HashMap();
//		map.put("name", "1111111");
//		map.put("date", 1);
//		map.put("amount", "321");
//		entity.setObj(map);
//		list.add(entity);
//
//		ElasticsearchEntity entity1 = new ElasticsearchEntity();
//		entity1.setTable_name(table_name);
//		entity1.setRow_id("123");
//		Map map1 = new HashMap();
//		map1.put("name", "fds");
//		map1.put("date", 321);
//		map1.put("amount", "sdf");
//		entity1.setObj(map1);
//		list.add(entity1);
//		System.out.println("add   " + SearchEngineAdapterClient.getSearchEngineAdapter().save(list));
//	}
//
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	@Test
//	public void update(){
//		Map map1 = new HashMap();
//		map1.put("like", "111");
//
//		Map map = new HashMap();
//    	map.put("name", "222");
//		map.put("inner",map1);
//		System.out.println("update  " + SearchEngineAdapterClient.getSearchEngineAdapter().update(table_name, row_id, map));
//	}
//
//	@Test
//	public void update_query(){
//		Map<String ,String> params_map = new HashMap();
//		params_map.put("amount","321");
//
//		Map map = new HashMap();
//		map.put("amount", "4444");
//		System.out.println("update_query   " + SearchEngineAdapterClient.getSearchEngineAdapter().update_query(params_map,map));
//	}
//
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	@Test
//	public void delete(){
//		System.out.println("delete   " + SearchEngineAdapterClient.getSearchEngineAdapter().delete(table_name, row_id));
//	}
//
//	@Test
//	public void delete_query(){
//		Map<String ,String> map = new HashMap();
//		//map.put("amount","es range 0 19");
//		map.put("amount","321");
//		System.out.println("delete_query   " + SearchEngineAdapterClient.getSearchEngineAdapter().delete_query(map));
//	}

    
    @Test
    public void querys(){
//    String ELASTICSEARCH_HOST = "10.139.52.151:9300,10.139.52.165:9300";
//    String [] ipAndPort=ELASTICSEARCH_HOST.split(",");
//    for(String hostName : ipAndPort){
//    	String  ip=hostName.split(":")[0];
//    	Integer port=Integer.valueOf(hostName.split(":")[1]);
//		System.out.println(ip+"ip"+" port:"+port);
//		String[] ipAndPort = hostName.split(":");
//		System.out.println(ipAndPort[0]+"  "+Integer.valueOf(ipAndPort[1]));  	
//	}
    	
    	String ELASTICSEARCH_HOST = "10.139.52.151:9300,10.139.52.165:9300";
    	String [] clusterHostArray=ELASTICSEARCH_HOST.split(",");
		for(String hostName : clusterHostArray){
			String  ip=hostName.split(":")[0];
			Integer port=Integer.valueOf(hostName.split(":")[1]);
			System.out.println(ip+"   "+port);
		}
    	
    	
    	
    }
    

	@Test
	public void query(){
		Map<String ,String> map = new HashMap();
		//范围查询
		//map.put("amount","es range 0 19");
//		map.put("amount","es range 0 19");
		//精确查询
//		map.put("date","es equals 122222222234561");
		//组合查询
//		map.put("info_type","es equals 2");
//		map.put("send_time","es range 1457940952000 1496129752000");
//		map.put("broker_id","es equals 105");
//		List<Map> l= SearchEngineAdapterClient.getSearchEngineAdapter().query(map);

		
//		history,
//		"{info_type=es equals 2, send_time=es range 1457940952000 1496129752000, broker_id=es equals 105}",
//		10,"send_time","desc"
		
		//分页，排序查询
//		List<Map> l= SearchEngineAdapterClient.getSearchEngineAdapter().query("test_type",map,0,10,"amount","desc");
//		m.put("send_time","es range 1426318552000 1464593752000");
//		List<Map> l= SearchEngineAdapterClient.getSearchEngineAdapter().query("history",map,0,10,"send_time","desc");
//		
//		
//		System.out.println(l);
		
		
//		Object s = SearchEngineAdapterClient.getSearchEngineAdapter().deleteAll();
		
//		System.out.println(s.toString()+" ss");
		
		Map params = new HashMap();
		
//		params.put("orCondition", "es should viewpoint_content == "
//				+ "h" + " or broker_name == " + "h"
//				+ " or customer_name == " + "h"
//				+ " or answer_content == " + "h"
//				+ " or viewpoint_abstract == " + "h"
//				+ " or tag_name == " + "h");
		
//		 params.put("position_str_long", "14703889527252199" );
    
		
//		params.put(null, "测试" );
		Object listSearch = SearchEngineAdapterClient.getSearchEngineAdapter().query("history", params, 0, 10,"send_time", "DESC");
          System.out.println(listSearch);
	}
}
